﻿using System;
using System.Net;
using System.Net.Sockets;


namespace Esst.Dto
{
    /// <summary>
    /// 客户端信息
    /// </summary>
    public class ClientInfo : IEquatable<ClientInfo>, ICloneable
    {
        /// <summary>
        /// 用户的IP地址
        /// </summary>
        public string IP { get; set; }
        /// <summary>
        /// 客户端类型
        /// </summary>
        public int ClientType { get; set; }
        /// <summary>
        /// 请求的网站地址
        /// </summary>
        public Uri Host { get; set; }
        /// <summary>
        /// 请求来源地址
        /// </summary>
        public Uri Origial { get; set; }

        /// <summary>
        /// 复制数据
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            return MemberwiseClone();
        }

        /// <summary>
        /// 复制数据
        /// </summary>
        /// <returns></returns>
        public ClientInfo Copy()
        {
            return Clone() as ClientInfo;
        }

        public bool Equals(ClientInfo other)
        {
            if (other is null)
                return false;

            return other.IP == IP
                && other.Host == Host
                && other.Origial == Origial
                && other.ClientType == ClientType;
        }

        public override bool Equals(object obj)
        {
            if (obj is null || obj is not ClientInfo target)
                return false;

            return Equals(target);
        }

        public override int GetHashCode()
        {
            return ClientType.GetHashCode();
        }

        ///// <summary>
        ///// 获取长整型的IP地址，仅IPV4访问下可用
        ///// </summary>
        ///// <returns></returns>
        //public long? GetLongIP()
        //{
        //    if (!IPAddress.TryParse(IP, out var address))
        //        return null;

        //    if (address.AddressFamily != AddressFamily.InterNetwork)
        //        return null;

        //    // 网络地址使用大端序
        //    var oriarray = address.GetAddressBytes();
        //    return oriarray[0] * 256 * 256 * 256
        //         + oriarray[1] * 256 * 256
        //         + oriarray[2] * 256
        //         + oriarray[3];
        //}
    }
}